package com.ruoyi.data.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.core.validate.EditGroupOne;
import com.ruoyi.common.enums.ClassificationType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.data.domain.DataClassification;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.data.domain.vo.DataClassificationVo;
import com.ruoyi.data.domain.bo.DataClassificationBo;
import com.ruoyi.data.service.IDataClassificationService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分类管理
 *
 * @author ruoyi
 * @date 2023-10-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/dataClassification/dataClassification")
public class DataClassificationController extends BaseController {

    private final IDataClassificationService iDataClassificationService;

    /**
     * 查询分类管理列表
     */
    @SaCheckPermission("dataClassification:dataClassification:list")
    @GetMapping("/list")
    public TableDataInfo<DataClassification> list(DataClassificationBo bo, PageQuery pageQuery) {
        return iDataClassificationService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询全部分类
     * @param bo
     * @return
     */
    @PostMapping("/allList")
    public R<List<DataClassificationVo>> allList(@RequestBody DataClassificationBo bo) {
        return R.ok(iDataClassificationService.queryList(bo));
    }

    /**
     * 导出分类管理列表
     */
    @SaCheckPermission("dataClassification:dataClassification:export")
    @Log(title = "分类管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DataClassificationBo bo, HttpServletResponse response) {
        List<DataClassificationVo> list = iDataClassificationService.queryList(bo);
        ExcelUtil.exportExcel(list, "分类管理", DataClassificationVo.class, response);
    }

    /**
     * 获取分类管理详细信息
     *
     * @param classificationId 主键
     */
    @SaCheckPermission("dataClassification:dataClassification:query")
    @GetMapping("/{classificationId}")
    public R<DataClassification> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long classificationId) {
        return R.ok(iDataClassificationService.queryById(classificationId));
    }

    /**
     * 新增分类管理
     */
    @SaCheckPermission("dataClassification:dataClassification:add")
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DataClassificationBo bo) {
        validatedData(bo);
        return toAjax(iDataClassificationService.insertByBo(bo, LoginHelper.getUserId()));
    }

    /**
     * 修改分类管理
     */
    @SaCheckPermission("dataClassification:dataClassification:edit")
    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DataClassificationBo bo) {
        validatedData(bo);
        return toAjax(iDataClassificationService.updateByBo(bo));
    }

    @SaCheckPermission("dataClassification:dataClassification:edit")
    @Log(title = "修改共享用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/editDataClassificationUser")
    public R<Void> editDataClassificationUser(@Validated(EditGroupOne.class) @RequestBody DataClassificationBo bo) {
        validatedData(bo);
        return toAjax(iDataClassificationService.editDataClassificationUser(bo));
    }

    /**
     * 删除分类管理
     *
     * @param classificationIds 主键串
     */
    @SaCheckPermission("dataClassification:dataClassification:remove")
    @Log(title = "分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{classificationIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] classificationIds) {
        Long userId = LoginHelper.getUserId();
        return toAjax(iDataClassificationService.deleteWithValidByIds(userId, Arrays.asList(classificationIds), false));
    }



    /**
     //非管理员不能添加修改共享分类
     * @param bo
     */
    private void validatedData(DataClassificationBo bo){

//        if (StringUtils.equals(ClassificationType.SHARE.getClassificationType(), bo.getClassificationType())){
//            if (!LoginHelper.isAdmin()){
//                throw new ServiceException("不是管理员无法操作");
//            }
//        }
    }
}
